package com.alawn.web.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ocpsoft.rewrite.annotation.Join;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alawn.framework.core.web.faces.annotation.MessageRequired;
import com.alawn.framework.core.web.faces.annotation.MessageType;
import com.alawn.framework.core.web.faces.controller.FacesController;
import com.alawn.framework.services.system.DictClassify;
import com.alawn.framework.services.system.Dictionary;
import com.alawn.system.service.DictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.Getter;
import lombok.Setter;

@Controller("/views/system/dictionary.faces")
@Join(path = "/system/dictionary", to = "/views/system/dictionary.faces")
public class DictionaryController extends FacesController {
	@Autowired
	private DictionaryService dictionaryService;

	@Getter
	@Setter
	private List<Dictionary> selectedDicts;

	@Override
	public void initPage() {
		dataList();

	}

	public void dataList() {
		List<DictClassify> classifies = dictionaryService.listClassify(null);
		Map<Long, DictClassify> classifyMap = new HashMap<>();
		classifies.forEach(classify -> {
			classifyMap.put(classify.getId(), classify);
		});
		putViewScope("dictionaries",
				dictionaryService.list(new QueryWrapper<Dictionary>().lambda().orderByAsc(Dictionary::getSeq)));
		putViewScope("classifies", classifies);
		putViewScope("classifyMap", classifyMap);
		selectedDicts = new ArrayList<>();
	}

	public void classifyList() {
		List<DictClassify> classifies = dictionaryService.listClassify(null);
		putViewScope("classifies", classifies);
	}

	public void addClassify() {
		putViewScope("classify", new DictClassify());
	}

	@MessageRequired(type = MessageType.SAVE)
	public void saveClassfiy() {
		DictClassify classify = (DictClassify) getViewScope("classify");
		dictionaryService.saveClassify(classify);
		dataList();
	}

	@MessageRequired(type = MessageType.SAVE)
	public void editClassify() {
		DictClassify classify = (DictClassify) getViewScope("classify");
		dictionaryService.saveClassify(classify);
		dataList();
	}
	
	
	@MessageRequired(type = MessageType.DELETE)
	public void deleteClassfiy() {
		DictClassify classify = (DictClassify) getViewScope("classify");
		dictionaryService.deleteByClassifyId(classify.getId());
		dataList();
	}

	public void add() {
		Dictionary dictionary = new Dictionary();
		putViewScope("dictionary", dictionary);
		classifyList();
	}

	@MessageRequired(type = MessageType.SAVE)
	public void save() {
		Dictionary dictionary = (Dictionary) getViewScope("dictionary");
		dictionaryService.saveOrUpdate(dictionary);
		dataList();
	}

	public void edit() {
		Dictionary dictionary = selectedDicts.size() > 0 ? selectedDicts.get(0) : null;
		if (dictionary != null) {
			dictionary = dictionaryService.getById(dictionary.getId());
		}
		putViewScope("dictionary", dictionary);
	}

	@MessageRequired(type = MessageType.DELETE)
	public void delete() {
		List<Long> idList = new ArrayList<>();
		selectedDicts.forEach(dictionary -> {
			idList.add(dictionary.getId());
		});
		dictionaryService.removeByIds(idList);
		dataList();
	}

}
